package com.googlecode.recycled.lang.jse.api.concurrent;

import com.googlecode.recycled.lang.assertions.Assert;
import org.apache.commons.lang.UnhandledException;

import java.util.concurrent.TimeUnit;

/**
 * @author Henryk Konsek
 */
public class TimeUnitSleeper implements Sleeper {

    final protected TimeUnit timeUnit;

    final protected long amount;

    public TimeUnitSleeper(TimeUnit timeUnit, long amount) {
        this.timeUnit = Assert.notNull(timeUnit);
        this.amount = Assert.notNull(amount);
    }

    public TimeUnitSleeper(long amount) {
        this(TimeUnit.SECONDS, amount);
    }

    @Override
    public void sleep() {
        try {
            timeUnit.sleep(amount);
        } catch (InterruptedException e) {
            throw new UnhandledException(e);
        }
    }

}
